package com.example.gw_back_end.controller;

import com.example.gw_back_end.domain.Result;
import com.example.gw_back_end.domain.entities.OrderItem;
import com.example.gw_back_end.service.OrderItemService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("order_item")
public class OrderItemController {

    private final OrderItemService orderItemService;
    public OrderItemController(OrderItemService orderItemService) {
        this.orderItemService = orderItemService;
    }

    // 订单新增商品
    @PostMapping("add_order_item")
    public Result addOrderItem(OrderItem orderItem) {
        if (orderItemService.addOrderItem(orderItem)) {
            return Result.success();
        } else {
            return Result.error();
        }
    }

    // 订单删除商品
    @DeleteMapping("delete_order_item")
    public Result deleteOrderItem(Integer orderItemId) {
        if (orderItemService.deleteOrderItem(orderItemId)) {
            return Result.success();
        } else {
            return Result.error();
        }
    }

    // 订单更新商品
    @PutMapping("update_order_item")
    public Result updateOrderItem(OrderItem orderItem) {
        if (orderItemService.updateOrderItem(orderItem)) {
            return Result.success();
        } else {
            return Result.error();
        }
    }

    // 订单查询商品
    @GetMapping
    public Result getOrderItem(Integer orderId) {
        List<OrderItem> OrderItmeList = orderItemService.getOrderItemById(orderId);
        if (OrderItmeList.toArray().length > 0) {
            return Result.success(OrderItmeList);
        } else {
            return Result.error();
        }
    }
}
